<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>
<body>
    <div class="nav">
        <img src="image/logo.jpg" alt="logo">
        <span class="title1">我的博客系统</span>
        <a href="home.html">主页</a>
        <a href="personlist.html">我的</a>
        <a href="edit.html">创作</a>
        <a href="logout">注销</a>
    </div>
    <div class="leader">
        <div class="empOne">
            <input type="text" class="title" > 
            <input type="button" value="发布文章" class="publish">
        </div>
        <div id="editor">
            <textarea name="content" style="display: none;" class="content"></textarea>
        </div>
    </div>

    <script>
        $.ajax({
            url: "updateLoad"+location.search,
            method: "GET",
            success: function(data,status) {
                if(data.message == null) {
                    let title = document.querySelector('.title');
                    title.value=data.title;
                    let content = document.querySelector('.content');
                    content.value=data.content;
                }else{
                    alert(data.message);
                    location.assign('home.html');
                }
            }
        })

        // 初始化编辑器
        var editor = editormd("editor", {
            // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉.
            width: "100%",
            // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
            height: "calc(100% - 60px)",

            // 指定 editor.md 依赖的插件路径
            path: "editor.md/lib/",
            // 放到 textarea中
            saveHTMLToTextArea: true
        });

        let submit = document.querySelector('.publish');
        submit.onclick = function() {
            let title = document.querySelector('.title');
            let content = document.querySelector('.content');
            if(title.value.trim() == ""){
                alert('当前文章标题为空,请输入!');
                title.focus();
                return;
            }
            if(content.value.trim() == ""){
                alert('当前文章内容为空,请输入!');
                content.focus();
                return;
            }
            $.ajax({
                url: "update"+location.search,
                method: "POST",
                data: JSON.stringify({title: title.value.trim(), content: content.value.trim()}),
                contentType: "application/json;charset=utf-8",
                success: function(data,status) {
                    if(data.message == null){
                        location.assign('home.html');
                    }else{
                        alert(data.message);
                        location.assign('home.html');
                    }
                }
            })
        }


    </script>
</body>
</html>